package com.example.Controller;

import com.example.Mapper.FriendMapper;
import com.example.component.OnlineUserManager;
import com.example.API.AddFriendRequest;
import com.example.pojo.Friend;
import com.example.pojo.Result;
import com.example.pojo.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
public class FriendController {
    @Resource
    private FriendMapper friendMapper;
    @Resource
    private OnlineUserManager onlineUserManager;

    private ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/friendList")
    @ResponseBody
    public Result friendList(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute("user");
        int userId = user.getId();
        List<Friend> friends = friendMapper.selectFriendList(userId);
        return Result.success(friends);
    }
    @GetMapping("/findFriend")
    @ResponseBody
    public Result friendAdd(String name,HttpServletRequest request) {
        try {
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute("user");
            List<Friend> findFriends = friendMapper.findFriend(user.getId(), name);
            return Result.success(findFriends);
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        return Result.success(null);
    }
    @GetMapping("/addFriend")
    @ResponseBody
    public Result addFriend(int friendId, String reason, HttpServletRequest req) {
        try {
            HttpSession session = req.getSession(false);
            User user = (User) session.getAttribute("user");
            System.out.println("[添加好友] " + user.getId() + ", " + friendId +
                    ", " + reason);
            // 写⼊到数据库
            friendMapper.addFriendRequest(user.getId(), friendId, reason);
            // 发送 websocket 响应
            WebSocketSession webSocketSession =
                    onlineUserManager.getSession(friendId);
            if (webSocketSession != null) {
                AddFriendRequest addFriendRequest = new AddFriendRequest();
                addFriendRequest.setType("addFriendRequest");
                addFriendRequest.setFromUserId(user.getId());
                addFriendRequest.setFromUserName(user.getUsername());
                addFriendRequest.setReason(reason);
                // 给对应的⽤⼾发送 websocket 请求, 提⽰有⽤⼾尝试加好友.
                webSocketSession.sendMessage(new
                        TextMessage(objectMapper.writeValueAsString(addFriendRequest)));
            }
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
        return Result.success(null);
    }
    @GetMapping("/getFriendRequest")
    @ResponseBody
    public Object getFriendRequest(HttpServletRequest req) {
        List<AddFriendRequest> addFriendRequestList = new ArrayList<>();
        try {
            HttpSession session = req.getSession(false);
            User user = (User) session.getAttribute("user");
            addFriendRequestList = friendMapper.getFriendRequest(user.getId());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return addFriendRequestList;
    }
    @GetMapping("/acceptFriend")
    @ResponseBody
    @Transactional
    public Result acceptFriend(int friendId, HttpServletRequest req) {
        try {
            HttpSession session = req.getSession(false);
            User user = (User) session.getAttribute("user");
            // 1. 删除好友请求表的存档
            friendMapper.deleteFriendRequest(friendId, user.getId());
            // 2. 修改好友表
            friendMapper.addFriend(friendId, user.getId());
            // 3. 通过 websocket 通知对⽅刷新好友列表
            WebSocketSession webSocketSession =
                    onlineUserManager.getSession(friendId);
            if (webSocketSession != null) {
                AddFriendRequest addFriendRequest = new AddFriendRequest();
                addFriendRequest.setType("acceptFriend");
                addFriendRequest.setFromUserId(user.getId());
                addFriendRequest.setFromUserName(user.getUsername());
                addFriendRequest.setReason("");
                webSocketSession.sendMessage(new
                        TextMessage(objectMapper.writeValueAsString(addFriendRequest)));
            }
        } catch (NullPointerException | IOException e) {
            e.printStackTrace();
        }
        return Result.success(null);
    }
    @GetMapping("/rejectFriend")
    @ResponseBody
    public Result rejectFriend(int friendId, HttpServletRequest req) {
        try {
            HttpSession session = req.getSession(false);
            User user = (User) session.getAttribute("user");
            // 删除好友请求表的存档
            friendMapper.deleteFriendRequest(friendId, user.getId());
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return Result.success(null);
    }

}
